package mapper;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.TransactionIsolationLevel;
import org.junit.Before;
import org.junit.Test;

import cn.itcast.mybatis.mapper.UserMapper;
import cn.itcast.mybatis.po.User;
import cn.itcast.mybatis.po.UserCustom;
import cn.itcast.mybatis.po.UserQueryVo;

/**
 *  使用classPath:mapper/usermapper.xml 为映射文件
 * @author E440
 *
 */
public class MapperTestDemo2 {
	private SqlSessionFactory factory=null;

	@Before
	public void beforeInstance() throws IOException{
		String config="config/sqlmapconfig.xml";
		InputStream in=Resources.getResourceAsStream(config);
		factory=new SqlSessionFactoryBuilder().build(in);
	}
	
	@Test
	public void testFindUserById() throws Exception {
		SqlSession session=factory.openSession();
		//创建UserMapper对象，mybatis自动生成mapper代理对象
		UserMapper mapper=session.getMapper(UserMapper.class);
		User user=mapper.findUserById(10);
		System.out.println(user);
		session.close();
	}
	
	@Test
	public void testFindUserByName() throws Exception {
		SqlSession sqlSession = factory.openSession();
		//创建UserMapper对象，mybatis自动生成mapper代理对象
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		//调用userMapper的方法
		List<User> list = userMapper.findUserByName("小明");
		sqlSession.close();
		System.out.println(list);
	}
	
	@Test
	public void testFindUserCount() throws Exception {
		
		SqlSession sqlSession = factory.openSession();
		
		//创建UserMapper对象，mybatis自动生成mapper代理对象
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		
		//创建包装对象，设置查询条件
		UserQueryVo userQueryVo = new UserQueryVo();
		UserCustom userCustom = new UserCustom();
		userCustom.setSex("1");
		userCustom.setUsername("张三丰");
		userQueryVo.setUserCustom(userCustom);
		//调用userMapper的方法
		int count = userMapper.findUserCount(userQueryVo);
		System.out.println(count);
	}
	
	//用户信息的综合 查询
	@Test
	public void testFindUserList() throws Exception {
		SqlSession sqlSession = factory.openSession();
		//创建UserMapper对象，mybatis自动生成mapper代理对象
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		//创建包装对象，设置查询条件
		UserQueryVo userQueryVo = new UserQueryVo();
		UserCustom userCustom = new UserCustom();
		//由于这里使用动态sql，如果不设置某个值，条件不会拼接在sql中
//			userCustom.setSex("1");
		userCustom.setUsername("小明");
		//传入多个id
		List<Integer> ids = new ArrayList<Integer>();
		ids.add(1);
		ids.add(10);
		ids.add(16);
		//将ids通过userQueryVo传入statement中
		userQueryVo.setIds(ids);
		userQueryVo.setUserCustom(userCustom);
		//调用userMapper的方法
		List<UserCustom> list = userMapper.findUserList(userQueryVo);
		System.out.println(list);
	}
	
	@Test
	public void testFindUserByIdResultMap() throws Exception {
		SqlSession sqlSession = factory.openSession();
		//创建UserMapper对象，mybatis自动生成mapper代理对象
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		//调用userMapper的方法
		User user = userMapper.findUserByIdResultMap(1);
		System.out.println(user);
	}
}
